<?php

// 接收前端传递过来的商品信息，插入到数据库里
$name = $_GET['name'];
$price = $_GET['price'];
$num = $_GET['num'];

// 从所有商品从数据库里取出来响应给前端
$con = mysqli_connect('localhost', 'root', 'root');

// 2、选择要操作的数据库
mysqli_select_db($con, '2004');

// 3、设置编码
mysqli_query($con, "set charset 'utf8'");
mysqli_query($con, "set character set 'utf8'");

$sql = "insert into shop (name,price,num) values ('$name',$price,$num)";

$res = mysqli_query($con, $sql);

if ($res) {
  // 新增成功
  echo json_encode(array(
    "code" => 200,
    "body" => array(
      "msg" => "录入商品成功"
    )
  ));
} else {
  echo json_encode(array(
    "code" => 0,
    "body" => array(
      "msg" => "网络错误，请重试"
    )
  ));
}

?>
